package com.itheima.service.Impl;

import com.itheima.common.constant.MessageConstant;
import com.itheima.common.entity.Result;
import com.itheima.common.utils.POIUtils;
import com.itheima.mapper.OrderSettingMapper;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class OrderSettingServiceImpl implements OrderSettingService {
    @Autowired
    private OrderSettingMapper orderSettingMapper;
    @Override
    public Result editNumberByOrderDate(OrderSetting orderSetting) {
      int  count = orderSettingMapper.editNumberByOrderDate(orderSetting);
      return count > 0 ? new Result(true, MessageConstant.ORDERSETTING_SUCCESS)
              : new Result(false, MessageConstant.ORDERSETTING_FAIL);

    }

    @Override
    public Result getOrderSettingByMonth(String month) {
        try {
            String[] split = month.split("-");
            Integer year = Integer.parseInt(split[0]);
            Integer month1 = Integer.parseInt(split[1]);
            //获取参数年月的第一天和最后一天
            LocalDate startDay = LocalDate.of(year, month1, 1);
            LocalDate endDay = startDay.withDayOfMonth(startDay.lengthOfMonth());
            List<Map<String, Object>> result = orderSettingMapper.getOrderSettingByMonth(startDay, endDay)
                    .stream()
                    .map(os -> {
                        Map<String, Object> map = new HashMap<>();
                        map.put("date", os.getOrderDate().getDayOfMonth()); //加工数据将订单日期只保留日
                        map.put("number", os.getNumber());
                        map.put("reservations", os.getReservations());
                        return map;
                    })
                    .collect(Collectors.toList());

            return new Result(true, MessageConstant.GET_ORDERSETTING_SUCCESS, result);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_ORDERSETTING_FAIL);
        }
    }

    @Override
    public Result upload(MultipartFile excelFile) {
        try {
            List<String[]> dataList = POIUtils.readExcel(excelFile);
            List<OrderSetting> settingList = new ArrayList<>();
            System.out.println(dataList);
            for (String[] data : dataList) {
                if (data.length < 2) continue;//跳过不合法的数据
                settingList.add(new OrderSetting(LocalDate.parse(data[0],
                        DateTimeFormatter.ofPattern("yyyy-MM-dd")),Integer.parseInt(data[1])));
                orderSettingiImport(settingList);//批量导入
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.IMPORT_ORDERSETTING_FAIL);
        }
        return new Result(true, MessageConstant.IMPORT_ORDERSETTING_SUCCESS);
    }
    public void orderSettingiImport(List<OrderSetting> list) {
        for (OrderSetting os : list) {
            // 先判断该日期是否已存在，存在则更新，不存在则插入
            int count = orderSettingMapper.countByOrderDate(os.getOrderDate());
            if (count > 0) {
                orderSettingMapper.updateNumberByOrderDate(os);
            } else {
                orderSettingMapper.insert(os);
            }
        }
    }
}
